<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
    <el-col>
      <strong v-if="isRoot">
        <i
          class="el-icon-office-building
"
        ></i>{{ treeNode.name }}  </strong>
      <span v-else>
        {{ treeNode.name }}
      </span>
    </el-col>
    <el-col :span="7">
      <el-row type="flex">
        <!-- 两个内容 负责人和操作 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 -->
          <el-dropdown>
            <span class="el-dropdown-link">操作
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <!-- 下拉菜单里的数据 -->
            <el-dropdown-menu>
              <el-dropdown-item @click.native="addDepartments">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                @click.native="editDepartments"
              >查看部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                @click.native="delDepartments"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col></el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

    }
  },

  methods: {
    // 删除部门
    async delDepartments() {
      // 二次询问
      await this.$confirm('确定要删除吗？')
      // 调用api接口 删除数据
      await delDepartments(this.treeNode.id)
      // 提示用户
      this.$message.success('删除成功')
      // 通知父组件 更新页面
      this.$emit('delDepartments')
    },

    // 添加子部门
    addDepartments() {
      // 传递treeNode节点
      this.$emit('addDepartments', this.treeNode)
    },

    // 查看/编辑功能
    editDepartments() {
      this.$emit('editDepartments', this.treeNode)
    }
  }
}
</script>
